package com.sosee.cgfw.friends;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sosee.cgfw.R;
import com.sosee.cgfw.utils.Constans;
import com.sosee.cgfw.utils.Utils;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.umeng.analytics.MobclickAgent;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Thinkpad on 2016/3/9.
 */
public class FriendsScanResult extends Activity implements View.OnClickListener {
    private HttpUtils httpUtils;
    private TextView my_nicknames_scan;
    private TextView my_names_scan;
    private RelativeLayout setting_info;
    private TextView my_tels_scan;
    private TextView my_companys_scan;
    private TextView my_dutys_together;
    private Button my_messages_scan;
    private ImageButton my_delete;

    private Intent intent;
    private ImageButton friends_back16;
    private String together;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.friends_scan_infos);

        intent = getIntent();
        together = intent.getStringExtra("togethers");


        System.out.println("这是传递过来的id：" + together);

        httpUtils = new HttpUtils();
        initData();
    }

    private void initData() {
        my_names_scan = (TextView) findViewById(R.id.my_names_scan);
        my_nicknames_scan = (TextView) findViewById(R.id.my_nicknames_scan);

        my_tels_scan = (TextView) findViewById(R.id.my_tels_scan);
        my_companys_scan = (TextView) findViewById(R.id.my_companys_scan);
        my_dutys_together = (TextView) findViewById(R.id.my_dutys_scan);

        friends_back16 = (ImageButton) findViewById(R.id.friends_back16);


        my_messages_scan = (Button) findViewById(R.id.my_messages_scan);


        initView(together);

        friends_back16.setOnClickListener(this);
        my_messages_scan.setOnClickListener(this);
    }

    private void initView(String id4) {
        System.out.print("这是请求链接:" + Constans.GETUSERINFO_URL1 + id4);
        httpUtils.send(HttpRequest.HttpMethod.GET, Constans.GETUSERINFO_URL1 + id4, new RequestCallBack<String>() {

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {

                String result = responseInfo.result;
                System.out.println("这是共同好友个人信息：" + result);

                Gson gson = new Gson();
                FriendsInfoData friendsInfoData = gson.fromJson(result, FriendsInfoData.class);
                if (null != friendsInfoData) {
                    my_names_scan.setText(friendsInfoData.getRealname());
                    ;//真实姓名
                    my_nicknames_scan.setText(friendsInfoData.getNickname());
                    my_tels_scan.setText(friendsInfoData.getPhone());
                    my_companys_scan.setText(friendsInfoData.getCompany());
                    my_dutys_together = (TextView) findViewById(R.id.my_duty);
                }

            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.friends_back16:
                //关闭
                finish();
                break;

            case R.id.my_messages_scan:
                //发送消息

                httpUtils.send(HttpRequest.HttpMethod.GET, Constans.ADDFRIENDS_URL + Constans.userID + "&friendsID=" + together, new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        String s = responseInfo.result;
                        System.out.println("这是返回的值" + s);
                        //发送消息到主线程通知更改  加为好友为等待验证。
//                        Utils.showToast(s);
                        try {
                            JSONObject json = new JSONObject(s);
                            String status = (String) json.get("status");
                            if (status == "success") {
                                Utils.showToast("请求以发送成功");
                                finish();
                            }else {
                                Utils.showToast("请求以发送成功");
                                finish();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }


                    }

                    @Override
                    public void onFailure(HttpException e, String s) {

                        Utils.showToast("网络获取异常");
                    }
                });

                break;


        }

    }


    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
